package com.jams.goodox.activity.home;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.entity.ServerBean;
import com.jams.goodox.entity.resp.AllServerListResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.MySwipeRefreshLayout;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * 首页全部服务
 *
 * create by 顾佳佳 on 2017/05/24
 */

public class AllServerActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.rcv_eat)
    RecyclerView rcvEat;
    @Bind(R.id.other_group)
    RelativeLayout otherGroup;
    @Bind(R.id.eat_group)
    RelativeLayout eatGroup;
    @Bind(R.id.rcv_other)
    RecyclerView rcvOther;

    List<ServerBean> eatServerList, otherServerList;
    @Bind(R.id.srf_all_server)
    MySwipeRefreshLayout srfAllServer;
    private RecycleCommonAdapter<ServerBean> eatServerAdapter, otherServerAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_all_server;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "全部服务");
        eatServerList = new ArrayList<>();
        otherServerList = new ArrayList<>();

        rcvEat.setLayoutManager(new GridLayoutManager(getContext(), 4));
        rcvOther.setLayoutManager(new GridLayoutManager(getContext(), 4));
        eatServerAdapter = new RecycleCommonAdapter<ServerBean>(getContext(), eatServerList, R.layout.server_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper,final ServerBean item, int position) {
                helper.setSimpleDraweeView(R.id.sdv_icon_image_item, item.getIconUrl())
                        .setText(R.id.tv_icon_name_item, item.getCustomName())
                        .setOnItemClickListener(new OnRecycleItemClickListener() {
                            @Override
                            public void itemClick() {
                                ServiceGo.gotoProductPage(AllServerActivity.this, item);
                            }
                        });
            }
        };
        otherServerAdapter = new RecycleCommonAdapter<ServerBean>(getContext(), otherServerList, R.layout.server_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final ServerBean item, int position) {
                helper.setSimpleDraweeView(R.id.sdv_icon_image_item, item.getIconUrl())
                        .setText(R.id.tv_icon_name_item, item.getCustomName())
                        .setOnItemClickListener(new OnRecycleItemClickListener() {
                            @Override
                            public void itemClick() {
                                ServiceGo.gotoProductPage(AllServerActivity.this, item);
                            }
                        });
            }
        };
        rcvEat.setAdapter(eatServerAdapter);
        rcvOther.setAdapter(otherServerAdapter);
        srfAllServer.setOnRefreshListener(this);
        onRefresh();
    }

    //获取数据
    private void getServerList() {
        enqueue(createCallService(HomeService.class).getAllServer(SePref.getCommunityId()), new RetrofitCallbackListener<AllServerListResp>() {
            @Override
            public void onSuccess(Call<AllServerListResp> call, AllServerListResp result) {
                srfAllServer.setRefreshing(false);
                if (result.getData() != null) {
                    if (CollectionUtils.hasData(result.getData().getEatServiceList())) {
                        eatServerList.clear();
                        eatServerList.addAll(result.getData().getEatServiceList());
                        eatServerAdapter.notifyDataSetChanged();
                        eatGroup.setVisibility(View.VISIBLE);
                    }else{
                        eatGroup.setVisibility(View.GONE);
                    }
                    if (CollectionUtils.hasData(result.getData().getOtherServiceList())) {
                        otherServerList.clear();
                        otherServerList.addAll(result.getData().getOtherServiceList());
                        otherServerAdapter.notifyDataSetChanged();
                        otherGroup.setVisibility(View.VISIBLE);
                    }else{
                        otherGroup.setVisibility(View.GONE);
                    }
                }else{
                    eatGroup.setVisibility(View.GONE);
                    otherGroup.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String msg) {
                srfAllServer.setRefreshing(false);
            }
        });
    }

    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                srfAllServer.setRefreshing(true);
                getServerList();
            }
        });
    }
}
